var orderDetailApp = angular.module("orderDetailApp", ["scService"]);
orderDetailApp.controller("orderDetailController", function($scope, $http, $q, scCodeService, $location, $state, $stateParams, pubFun) {
    //订单价格数据处理，从2处理成2.00格式
    $scope.intToFloat = function(num) {
            num = Math.round(num * 100) / 100;
            var obj = num.toString().split(".");
            if (obj.length == 1) {
                num = num + '.00';
            } else {
                if (obj[1].length == 1) {
                    num = num + '0';
                }
            }
            return num;
            
        }
    $scope.isWechatPay=true;
    $scope.isNotPay=false;
        //订单数据显示处理方法
    $scope.orderShow = function(data) {
            //判断订单状态，未支付则停留在此页，已支付则跳转至订单完成界面
            /*if(data.status=="7"){
            	$location.path("/orderDetail/"+data.chargeDetail.orderNo);
            }*/
            //显示支付金额
            $scope.isPay = true;
            $scope.notPay = false;
            if (data.caculatedTotalPrice == "0.00") {
                $scope.isWechatPay=false;
                $scope.isNotPay=true;
                $scope.isPay = false;
                $scope.notPay = true;
            }
            //计算充电费和服务费，优惠
            $scope.chargePrice = 0;
            $scope.servicePrice = 0;
            $scope.discountPrice = 0;
            $scope.reservationPrice = 0;
            $scope.chargeList = data.orderDetails;
            
            angular.forEach($scope.chargeList, function(data, index, array) {
                if (array[index].serviceNo == "100002") {
                    
                    $scope.chargePrice = parseFloat($scope.chargePrice + array[index].totalPrice);
                } else if (array[index].serviceNo == "100001") {
                    
                    $scope.servicePrice = parseFloat($scope.servicePrice + array[index].totalPrice);
                } else if (array[index].serviceNo == "100003") {
                    
                    $scope.reservationPrice = parseFloat($scope.servicePrice + array[index].totalPrice);
                }
                $scope.countPrice = parseFloat($scope.countPrice) + parseFloat(array[index].totalPrice);
                $scope.discountPrice = parseFloat($scope.discountPrice) + parseFloat(array[index].totalPrice - array[index].realTotalPrice);
            });

            $scope.reservationPrice = $scope.intToFloat($scope.reservationPrice);
            $scope.chargePrice = $scope.intToFloat($scope.chargePrice);
            $scope.servicePrice = $scope.intToFloat($scope.servicePrice);
            //判断优惠金额是否有值，有则显示，无则隐藏
            $scope.hasDiscount = false;
            if ($scope.discountPrice && $scope.discountPrice > 0) {
                $scope.hasDiscount = true;
                $scope.discountPrice = $scope.intToFloat($scope.discountPrice);
            }
            
            //将订单数据存储到本地localStorage里
            var ls = localStorage;
             //0
            ls.removeItem('payDetail');
             //清除存入数据
            ls.setItem('userinfo', JSON.stringify($scope.userinfo));
            ls.setItem('payDetail', JSON.stringify(data));
             //显示存入数据
            if (data.grade != undefined) {
                $scope.isEvalu = false;
                if (data.lables != undefined) {
                    $scope.lables = data.lables.split("#"); //.splice(0,1);
                    $scope.score = data.grade * 2;
                    $scope.fScore = globalFun.decimal(data.grade, 1);
                    $scope.lablesArr = [];
                    angular.forEach($scope.lables, function(data, index, array) {
                        if (array[index] != "") {
                            $scope.lablesArr.push(array[index]);
                        }

                    });
                }
            } else {
                $scope.isEvalu = true;
            }
        }
        //获取路由上的订单id调用接口获取订单详情
    $scope.getOrderDetailByOrderNo = function() {
        //alert("getOrderDetailByOrderNo开始调用")
        var deferred = $q.defer();
        //获取路由上获取订单id
        if ($stateParams.orderNo) {
            $scope.orderNo = $stateParams.orderNo;
        }
        var postData = {
            orderNo: $scope.orderNo
        }
        scCodeService.getOrderDetail(postData).then(function(data) {
            
            if (data.code == "200") {
                if (data.data) {
                    $scope.order = data.data;
                    //调用订单显示数据处理方法
                    $scope.orderShow($scope.order);
                    $scope.stationId = data.data.chargerDetail.stationId;
                    $scope.grade = data.data.grade;
                    localStorage.setItem("orderGrade",$scope.grade);
                    localStorage.setItem("orderStationId",$scope.stationId);
                    if($scope.grade!==undefined){
                        $scope.scoreStation();
                    }
                }
                deferred.resolve(data.data)
            }
        })
        return deferred.promise;
    }
    $scope.getOrderDetailByOrderNo();
    // 点击费用明细
    $scope.detail = function() {
        var orderNo = $stateParams.orderNo;
        $location.path("orderPayDetail/" + orderNo);
    };

    $scope.star = 0;
    $scope.wordsEvl;
    $scope.couldEvl = false;
    $scope.lables = "";
    //獲取用戶id
    var userId = localStorage.getItem("userId");
    $scope.evalu = function(index) {
        $scope.star = index;
        $scope.starDesc = (globalFun.starDesc)[index - 1];
        $scope.couldEvl = true;
    };
    $scope.market = function(index) {
        for (var i = 1; i < 5; i++) {
            var change = "change" + i;
            var lable = "lable" + i;
            if (index == i) {
                if ($scope[change] == index) {
                    $scope[change] = "";
                    $scope[lable] = ""
                } else {
                    $scope[lable] = globalFun.lables[index - 1];
                    $scope[change] = index;
                }
                $scope.lables += "#" + $scope[lable];
                break;
            }
        }
    };
    $scope.submitSug = function() {
        $scope.orderNo = $stateParams.orderNo;
        var postData = {
            orderNo: $scope.orderNo,
            lables: $scope.lables,
            grade: $scope.star,
            evaluate: $scope.wordsEvl,
            userId: userId
        };
        // alert(JSON.stringify(postData));
        if ($scope.couldEvl == true) {
            scCodeService.orderAttach(postData).then(function(data) {
                // alert(JSON.stringify(data));
                if (data.code == "200") {
                    $scope.toastSet({
                        content: "评价成功"
                    });
                    window.location.reload();
                }
            });
        }
    };
    // $scope.focus=function();
    $scope.wordsCtr = function() {
        if ($scope.wordsEvl.length > 300) {
            $scope.wordsEvl = $scope.wordsEvl.substr(0, 300);
            $scope.toastSet({
                content: "输入超长，请输入300个中文以内的意见或建议。"
            });

        };
    };
    //
    $scope.scoreStation = function(){
        $scope.stationId = localStorage.getItem("orderStationId");
        $scope.grade = localStorage.getItem("orderGrade");
        var postData = {
            userId:userId,
            stationId:$scope.stationId,
            score:$scope.grade
        }
        scCodeService.scoreStation(postData).then(function(data){
            if(data.code == "200"){
                console.log("ok");
            }
        })
    }
})
